<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
<!--        <el-form-item>-->
<!--          <el-select v-model="listPram.roles" placeholder="角色" clearable class="selWidth">-->
<!--            <el-option-->
<!--              v-for="item in roleList.list"-->
<!--              :key="item.id"-->
<!--              :label="item.roleName"-->
<!--              :value="item.id"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <!--  <el-form-item>
           <el-select v-model="listPram.status" placeholder="状态" clearable class="selWidth">
             <el-option
               v-for="item in constants.roleListStatus"
               :key="item.value"
               :label="item.label"
               :value="item.value"
             />
           </el-select>
         </el-form-item> -->
        <el-form-item label="关键词：">
          <el-input v-model="listPram.username" placeholder="请输入账号" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handlerOpenEdit(0)" v-hasPermi="['admin:system:admin:save']">添加管理员</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" v-loading="loading" size="mini" :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column
          prop="id"
          label="ID"
          width="50"
        />
        <el-table-column label="姓名" prop="realName" min-width="100"/>
        <el-table-column label="账号" prop="account" min-width="100"/>
        <el-table-column label="身份" prop="roleName" min-width="100">
          <template slot-scope="scope" v-if="scope.row.roleName">
            <el-tag size="small" type="info" v-for="(item, index) in scope.row.roleName.split(',')" :key="index" class="mr5">{{ item }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="联系电话" prop="phone" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.mobile | filterEmpty }}</span>
          </template>
        </el-table-column>
        <el-table-column label="最后登录时间" prop="lastLoginTime" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.lastLoginTime | filterEmpty }}</span>
          </template>
        </el-table-column>
        <el-table-column label="最后登录IP" prop="lastIp" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.lastIp | filterEmpty }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          min-width="100"
        >
          <template slot-scope="scope"  v-if="checkPermi(['admin:system:admin:update:status'])">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              active-text="开启"
              inactive-text="关闭"
              @change="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="130"  fixed="right">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" type="primary" size="mini" @click="handlerOpenEdit(1,scope.row)" v-hasPermi="['admin:system:admin:info']">编辑</el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="handlerOpenDel(scope.row)" v-hasPermi="['admin:system:admin:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.count"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
      :visible.sync="editDialogConfig.visible"
      :title="editDialogConfig.isCreate === 0? '创建管理员':'编辑管理员'"
      destroy-on-close
      :close-on-click-modal="false"
      width="700px"
    >
      <edit
        v-if="editDialogConfig.visible"
        :is-create="editDialogConfig.isCreate"
        :edit-data="editDialogConfig.editData"
        :all-tree-list="treeList"
        :all-role-list="roleList"
        @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import * as systemAdminApi from '@/api/systemadmin.js'
import * as categoryApi from "@/api/categoryApi.js";
import * as roleApi from '@/api/role.js'
import edit from './edit'
import { checkPermi } from "@/utils/permission";
import * as selfUtil from "@/utils/ZBKJIutil";
export default {
  // name: "index"
  components: { edit },
  data() {
    return {
      constants:this.$constants,
      loading:false,
      listData: { list: [] },
      listPram: {
        username: '',
        /*addTime: null,
        lastIp: null,
        lastTime: null,
        level: null,
        loginCount: null,
        realName: null,
        roles: null,
        status: null,*/
        page: 1,
        limit: this.$constants.page.limit[0]
      },
      treeList: [],
      roleList: [],
      menuList: [],
      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {}
      }
    }
  },
  mounted() {
    this.handleGetAdminList()
   this.handleGetRoleList() //用于筛选条件中角色下拉
    //this.handlerGetTreeList() //所属单位
  },
  methods: {
    checkPermi,
    onchangeIsShow(row) {
      systemAdminApi.updateStatusApi({id: row.id, status: row.status}).then(async () => {
          this.$message.success('修改成功');
          this.handleGetAdminList()
        }).catch(()=>{
        row.status = !row.status
      })
    },
    handleSearch() {
      this.listPram.page = 1
      this.handleGetAdminList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.handleGetAdminList()
      this.handleGetRoleList(this.listPram)
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.handleGetAdminList()
      this.handleGetRoleList(this.listPram)
    },
    handleGetRoleList() {
      const _pram = {
        page: 1,
        limit: this.constants.page.limit[4],
        status: 1
      }
      roleApi.getRoleList(_pram).then(data => {
        this.roleList = data
      })
    },

    //获取部门列表
    handlerGetTreeList() {
      const _pram = {
        name: '',
        page: 1,
        limit: this.constants.page.limit[4]
      }

      this.loading = true
      categoryApi.treeCategroy(_pram, 3).then(data => {
      //let data = this.constants.staticDepartmentList
        this.treeList = data.list
        this.loading = false
      }).catch(()=>{
        this.loading = false
      })
    },

    handlerOpenDel(rowData) {
      this.$confirm('确认删除当前数据').then(() => {
        const _pram = { id: rowData.id}
        systemAdminApi.adminDel(_pram).then(data => {
          this.$message.success('删除数据成功')
          this.handleGetAdminList()
        })
      })
    },
    handleGetAdminList() {
      systemAdminApi.adminList( this.listPram ).then(data => {
      // let data = {
      //   "count": 7,
      //   "list": [
      //     {
      //       "id": 1,
      //       "departmentId": 0,
      //       "account": "admin",
      //       "realName": "超级管理员",
      //       "phone": "",
      //       "roles": 1,
      //       "lastIp": "192.168.0.111",
      //       "lastLoginTime": "2023-03-20T06:56:33.000+00:00",
      //       "status": 1,
      //       "departmentName": "",
      //       "lastLoginTimeText": "2023-03-20 14:56:33",
      //       "roleName": "超级管理员"
      //     },
      //     {
      //       "id": 2,
      //       "departmentId": 2,
      //       "account": "玉门市城市管理综合执法局",
      //       "realName": "玉门市城市管理综合执法局管理员",
      //       "phone": "15809376063",
      //       "roles": 2,
      //       "lastIp": "192.168.0.111",
      //       "lastLoginTime": "2023-03-20T02:52:22.000+00:00",
      //       "status": 1,
      //       "departmentName": "玉门市城市管理综合执法局",
      //       "lastLoginTimeText": "2023-03-20 10:52:22",
      //       "roleName": "业务部门"
      //     },
      //     {
      //       "id": 3,
      //       "departmentId": 3,
      //       "account": "玉门市社会治安综合治理中心",
      //       "realName": "玉门市社会治安综合治理中心管理员",
      //       "phone": "13110021572",
      //       "roles": 2,
      //       "lastIp": "192.168.0.111",
      //       "lastLoginTime": "2023-03-20T02:22:33.000+00:00",
      //       "status": 1,
      //       "departmentName": "玉门市社会治安综合治理中心",
      //       "lastLoginTimeText": "2023-03-20 10:22:33",
      //       "roleName": "业务部门"
      //     },
      //   ]
      // }
        this.listData = data
        // this.handlerGetMenuList() //本身注释
      })
    },
    handlerOpenEdit(isCreate, editDate) {
      this.editDialogConfig.editData = editDate
      this.editDialogConfig.isCreate = isCreate
      this.editDialogConfig.visible = true
    },
    handlerGetMenuList() { // 获取菜单全部数据后做menu翻译使用
      systemAdminApi.listCategroy({ page: 1, limit: 999, type: 5 }).then(data => {
        this.menuList = data.list
        this.listData.list.forEach(item => {
          const _muneText = []
          const menuids = item.rules.split(',')
          menuids.map(muid => {
            this.menuList.filter(menu => {
              if (menu.id == muid) {
                _muneText.push(menu.name)
              }
            })
          })
          item.rulesView = _muneText.join(',')
          this.$set(item, 'rulesViews', item.rulesView)
        })
      })
    },
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.handleGetAdminList()
    },
    resetQuery(){
      this.listPram.username = '';
      this.handleGetAdminList()
    }
  }
}
</script>

<style scoped>

</style>
